package unit1_0929.test7;

import java.util.Scanner;

/*
 * 吃货联盟系统核心模块类.
 * 注:
 * Java里有4种访问权限修饰情况.
 * 访问权限最大的是public,公共访问权限,外部均可对其访问;
 * 访问权限最小的是private,私有访问权限,仅在类内部可以对其访问.
 */
public class OrderUnionSys {

	// 数据设计,存储订单及菜品信息
	private String[] orderNameArr = new String[7];// 订单用户名数组
	private String[] orderDishArr = new String[7];// 订单菜品数组.格式:糖醋鲤鱼 2份
	private String[] orderAddrArr = new String[7];// 订单地址数组
	private String[] orderDateArr = new String[7];// 订单日期数组.日期格式:yyyy-MM-dd
	private String[] orderTelArr = new String[7];// 订单电话数组
	private int[] orderStatusArr = new int[7];// 订单状态数组.规则:0已预订[默认];1已完成
	private double[] orderMoneyArr = new double[7];// 订单金额数组

	private String[] dishNameArr = new String[3];// 菜品名称数组
	private double[] dishPriceArr = new double[3];// 菜品价格数组
	private int[] dishPraiseArr = new int[3];// 菜品点赞数

	/*
	 *  仅向外暴露吃货联盟系统总的运行方法,让用户简洁易用.
	 *  工程师要做的事情是:把复杂难懂的逻辑封装起来,提供给用户一个极其简单的使用.
	 */
	public void run() {
		// 订单测试数据
		orderNameArr[0] = "张三";
		orderDishArr[0] = "糖醋鲤鱼 2份";
		orderAddrArr[0] = "北礼士路";
		orderDateArr[0] = "2020-09-30";
		orderTelArr[0] = "13929340856";
		orderMoneyArr[0] = 38.99 * 2;

		orderNameArr[1] = "李四";
		orderDishArr[1] = "麻婆豆腐 3份";
		orderAddrArr[1] = "北礼士路";
		orderDateArr[1] = "2020-09-30";
		orderTelArr[1] = "13919340856";
		orderStatusArr[1] = 1;
		orderMoneyArr[1] = 35.99 * 3;

		// 菜品测试数据
		dishNameArr[0] = "糖醋鲤鱼";
		dishPriceArr[0] = 38.99;

		dishNameArr[1] = "麻婆豆腐";
		dishPriceArr[1] = 35.99;

		dishNameArr[2] = "宫保鸡丁";
		dishPriceArr[2] = 32.99;

		// 定义6个变量,表示6种功能的名称
		String info1 = "我要订餐";
		String info2 = "查看餐袋";
		String info3 = "签收订单";
		String info4 = "删除订单";
		String info5 = "我要点赞";
		String info6 = "退出系统";

		// 创建扫描器
		Scanner scanner = new Scanner(System.in);
		// 定义循环条件;当变为false时,结束循环
		boolean flag = true;
		while (flag) {
			System.out.println("▓▓▓▓▓▓▓▓欢迎访问吃货联盟订餐系统▓▓▓▓▓▓▓▓");
			System.out.println(
					"1." + info1 + "\n2." + info2 + "\n3." + info3 + "\n4." + info4 + "\n5." + info5 + "\n6." + info6);
			System.out.print("请输入序号选择:");
			int choice = scanner.nextInt();
			// 等值判断choice
			switch (choice) {
			case 1:
				System.out.println("您选择的是:" + info1);
				// 调用点餐的方法,传入实参scanner.
				orderDish(scanner);
				break;
			case 2:
				System.out.println("您选择的是:" + info2);
				// 调用方法,显示订单信息
				showOrders();
				break;
			case 3:
				System.out.println("您选择的是:" + info3);
				// 调用签收订单的方法,传入实参scanner
				receiveOrder(scanner);
				break;
			case 4:
				System.out.println("您选择的是:" + info4);
				//调用删除订单的方法;传入实参scanner
				dropOrder(scanner);
				break;
			case 5:
				System.out.println("您选择的是:" + info5);
				// 调用点赞的方法
				praiseDish(scanner);
				break;
			case 6:
				System.out.println("您选择的是:" + info6 + "\n欢迎您下次光临!");
				// 将flag设置为false,结束while循环
				flag = false;
				break;
			default:
				System.out.println("输入非法,请重新输入!");
				break;
			}
		}
	}

	// 1.我要订餐
	private void orderDish(Scanner scanner) {
		for(int i=0;i<orderNameArr.length;i++) {
			if(orderNameArr[i]==null) {
				System.out.print("请输入订餐人姓名:");
				orderNameArr[i]=scanner.next();
				System.out.println("这些是当前的菜品信息:");
				//调用显示菜品信息的方法
				showDishes();
				System.out.print("请输入您要选择的菜品编号:");
				int index=scanner.nextInt();
				System.out.print("请输入您要点餐的份数:");
				int num=scanner.nextInt();
				orderDishArr[i]=dishNameArr[index-1]+" "+num+"份";
				orderMoneyArr[i]=dishPriceArr[i]*num;//计算出订单金额
				System.out.print("请输入送餐地址:");
				orderAddrArr[i]=scanner.next();
				System.out.print("请输入送餐日期[格式:yyyy-MM-dd]:");
				orderDateArr[i]=scanner.next();
				System.out.print("请输入订餐人电话:");
				orderTelArr[i]=scanner.next();
				System.out.println("恭喜您订餐成功,祝您用餐愉快!");
				//输入完一条订餐信息后,结束当前的for循环
				break;
			}
		}		
	}

	// 2.查看餐袋,即查看订单信息
	private void showOrders() {
		System.out.println("编号\t用户名\t所订菜品\t\t送货地址\t送货日期\t\t送货电话\t\t订单状态\t金额");
		for (int i = 0; i < orderNameArr.length; i++) {
			if (orderNameArr[i] != null) {
				System.out.println((i + 1) + "\t" + orderNameArr[i] + "\t" + orderDishArr[i] + "\t" + orderAddrArr[i]
						+ "\t" + orderDateArr[i] + "\t" + orderTelArr[i] + "\t"+(orderStatusArr[i] == 0 ? "已预订" : "已完成")
						+ "\t" + String.format("%.2f", orderMoneyArr[i]));
			}
		}
	}

	// 3.签收订单.传入形参scanner,该方法调用时传入实际的scanner扫描器即可.
	private void receiveOrder(Scanner scanner) {
		// 定义局部变量,标识是否存在需签收的订单;默认false不存在
		boolean exists = false;
		for (int i = 0; i < orderNameArr.length; i++) {
			if (orderNameArr[i] != null && orderStatusArr[i] == 0) {
				exists = true;
			}
		}
		if (exists) {
			System.out.println("编号\t用户名\t所订菜品\t\t送货地址\t送货日期\t\t送货电话\t\t订单状态\t金额");
			for (int i = 0; i < orderNameArr.length; i++) {
				if (orderNameArr[i] != null && orderStatusArr[i] == 0) {
					System.out.println((i + 1) + "\t" + orderNameArr[i] + "\t" + orderDishArr[i] + "\t" + orderAddrArr[i]
							+ "\t" + orderDateArr[i] + "\t" + orderTelArr[i] + "\t"+(orderStatusArr[i] == 0 ? "已预订" : "已完成")
							+ "\t" + String.format("%.2f", orderMoneyArr[i]));
				}
			}
			System.out.print("请输入您要签收的订单:");
			int index = scanner.nextInt();
			orderStatusArr[index - 1] = 1;// 标识签收订单
			System.out.println("恭喜您,完成订单签收!");
		} else {
			System.out.println("目前不存在需签收的订单!");
		}
	}

	//4.删除订单
	private void dropOrder(Scanner scanner) {
		//先查看当前的订单信息;调用查看餐袋方法
		showOrders();
		System.out.print("请输入您要删除的订单编号:");
		int index=scanner.nextInt();
		for(int i=index-1;i<=orderNameArr.length-1;i++) {
			//被删除元素之后的全部非null元素整体向前移动一位;末位元素置为null
			/*
			 * 如果遇到最后一个元素,且最后一个元素不为null,就置为null;
			 * 否则后一位元素覆盖前一位元素,一直遇到null为止,退出for循环.
			 */
			if(i==orderNameArr.length-1 && orderNameArr[i]!=null) {
				orderNameArr[i] = null;
				orderDishArr[i] = null;
				orderAddrArr[i] = null;
				orderDateArr[i] = null;
				orderTelArr[i] = null;
				orderStatusArr[i] = 0;
				orderMoneyArr[i] = 0;
			}else{
				orderNameArr[i] = orderNameArr[i+1];
				orderDishArr[i] = orderDishArr[i+1];
				orderAddrArr[i] = orderAddrArr[i+1];
				orderDateArr[i] = orderDateArr[i+1];
				orderTelArr[i] = orderTelArr[i+1];
				orderStatusArr[i] = orderStatusArr[i+1];
				orderMoneyArr[i] = orderMoneyArr[i+1];
				if(orderNameArr[i+1]==null) {
					break;
				}
			}
		}
	}
	
	// 5.我要点赞
	private void praiseDish(Scanner scanner) {
		// 调用显示全部菜品信息的方法
		showDishes();
		System.out.print("请输入您要点赞的的菜品编号:");
		int index = scanner.nextInt();
		dishPraiseArr[index - 1] += 1;// 成功点赞
		System.out.println("恭喜您点赞成功!感谢您对我们的支持,我们会继续努力!");
	}
	
	//由于菜品显示功能在不同方法里会被调用,因此单独封装显示菜品的方法,避免代码冗余
	private void showDishes() {
		System.out.println("编号\t菜品名称\t单价\t点赞数");
		for (int i = 0; i < dishNameArr.length; i++) {
			System.out.println((i + 1) + "\t" + dishNameArr[i] + "\t" + dishPriceArr[i] + "\t" + dishPraiseArr[i]);
		}
	}

}
